package com.jxaummd.mushroom.main.common.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.R2;
import com.jxaummd.mushroom.core.base.fragment.PresenterFragment;
import com.jxaummd.mushroom.main.common.contract.ForgetPasswordContract;
import com.jxaummd.mushroom.main.common.presenter.ForgetPasswordPresenter;


import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPasswordFragment extends PresenterFragment<ForgetPasswordContract.Presenter>
        implements ForgetPasswordContract.View {

    @BindView(R2.id.ec_tv_common_forget_password_timer)
    TextView mTextTimer = null;

    @BindView(R2.id.ec_tv_common_forget_password_hint)
    TextView mTextHint = null;

    @BindView(R2.id.ec_et_common_forget_password_testcode)
    EditText mEditTestCode = null;


    @BindView(R2.id.ec_et_common_forget_password_password)
    EditText mEditPassword = null;

    @BindView(R2.id.ec_et_common_forget_password_cetern_password)
    EditText mEditVerifyPassword = null;


    private String phoneNumber = "1561651";


    @OnClick(R2.id.ec_ib_common_forget_password_close)
    public void close() {
        pop();
    }

    @OnClick(R2.id.ec_btn_common_forget_password_submit)
    public void submit() {
        String code = mEditTestCode.getText().toString().trim();
        String password = mEditPassword.getText().toString().trim();
        String verifypassword = mEditVerifyPassword.getText().toString().trim();

        String hint = "";
        if (TextUtils.isEmpty(code)) {
            hint = "请将验证码填写完整";
        }
        if (TextUtils.isEmpty(password)) {
            hint = "请填写密码";
        }
        if (TextUtils.isEmpty(verifypassword)) {
            hint = "请确认密码";
        }
        if (!password.equals(verifypassword)) {
            hint = "两次密码要一致哦!";
        }
        if (hint.length() < 2) {
            mPresenter.resetPassword(phoneNumber, code, verifypassword);
        } else {
            ToastUtils.showShort(hint);
        }
    }

    public static ForgetPasswordFragment newInstane(String message) {
        ForgetPasswordFragment fragment = new ForgetPasswordFragment();
        fragment.phoneNumber = message;
        return fragment;
    }

    @Override
    protected ForgetPasswordContract.Presenter initPresenter() {
        return new ForgetPasswordPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.common_forget_password;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mPresenter.start();
        mTextHint.append(phoneNumber);
    }

    @Override
    public void updateTimer(String message) {
        if (mTextTimer != null)
            mTextTimer.setText(message);
    }

    @Override
    public void resetSuccess() {
        pop();
    }
}
